﻿using JGSoft.U9CRTAPIEX.Public;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.CBO.Pub.Controller;
using UFSoft.UBF.Business;
using UFSoft.UBF.PL;

namespace U9C.ExtendAPI.Public
{
    public static class PublicMethod
    {
        /// <summary>
        /// 控制器U9上下文写入
        /// </summary>
        /// <param name="_userCode"></param>
        /// <param name="_entCode"></param>
        /// <param name="_orgCode"></param>
        /// <param name="paras"></param>
        /// <param name="controllerName"></param>
        public static void WriteContext(string _userCode,string _entCode,string _orgCode,string paras,string controllerName)
        {
            ContextDTO tdto = new ContextDTO();
            tdto.UserCode = _userCode;//取接口传入的用户编码
            tdto.EntCode = _entCode;//取接口传入的企业编码
            tdto.CultureName = "zh-CN";//固定
            tdto.OrgCode = _orgCode;//取接口传入的组织编码
            tdto.WriteToContext();
            LogManager.WriteLog(paras, controllerName);
        }

        /// <summary>
        /// 将字符数组添加单引号并用逗号分割，用于sql查询
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string StringJoin(IEnumerable<string> args)
        {
            return string.Join(",", args.Select(t => $"'{t}'"));
        }

        /// <summary>  
        /// 计算字符串的MD5哈希值  
        /// </summary>  
        /// <param name="input">待计算的字符串</param>  
        /// <returns>MD5哈希值的十六进制字符串表示</returns>  
        public static string ComputeMD5Hash(string input)
        {
            // 创建一个MD5CryptoServiceProvider对象  
            using (MD5 md5Hash = MD5.Create())
            {
                // 将输入字符串转换为字节数组，并计算哈希数据  
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

                // 创建一个StringBuilder来收集字节  
                // 并将其转换为十六进制字符串  
                StringBuilder sBuilder = new StringBuilder();

                // 循环遍历哈希数据的每个字节  
                // 并格式化为两位数的十六进制数  
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }

                // 返回十六进制字符串  
                return sBuilder.ToString();
            }
        }

        /// <summary>
        /// U9实体批量in条件查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propName"></param>
        /// <param name="codes"></param>
        /// <returns></returns>
        public static List<T> GetDistinctElements<T>(string propName, IEnumerable<string> codes) where T : BusinessEntity
        {
            var elements = new List<T>();
            codes = codes.Distinct();
            if (codes.Any())
            {
                var finder = typeof(T).GetProperty("Finder").GetValue(null);
                var method = finder.GetType().GetMethod("FindAll", new Type[] { typeof(string), typeof(OqlParam[]) });
                string query = $"{propName} in ({StringJoin(codes)})";
                elements = ((IEnumerable)method.Invoke(finder, new object[] { query, new OqlParam[0] })).Cast<T>().ToList();
            }
            return elements;
        }
    }
}
